<?php
/**
 *  分享统计
 */

class ShareDetail extends APIBase {
	public $tag = "ShareDetail";
	public $isLogin = true;
	public function before() {
		$this->initMysql();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    $playerID = $this->uid;
		$time = time();
		$id = $this->param['mid'];
		//1为首次被邀请的邮件奖励 2为邀请人数的邮件奖励
		$type = $this->param['type'];
		
		$row0 = $this->mysql->select("sharecount","count(id) as num",array('topid'=>$playerID));
		$R = $row0[0]['num'];
        $invite = $R =5;
		
		$sharereward = $this->cache_redis->hGetAll("hsharereward:".$id);
		$sharenum = $sharereward['sharenum'];
		if($type==1 && $sharenum==0){
			$this->mysql->query("update sharecount set status=1 where sid=$playerID");
			$mid = $sharereward['mid'];

			$usermail = $this->cache_redis->keys("hUserEmailList:".$playerID.":*");
			if(empty($usermail)){
				$count = 0;
				$this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$id,'type'=>5));
			}else{
				$arr = array();
				for($i=0;$i<count($usermail);$i++){
					$row = $this->cache_redis->hMGet("hUserEmailList:".$playerID.":".$i,array('type','rewardid','date'));
					if($row['type']==5){
						$arr[] = $row;
					}
				}
				if(empty($arr)){
					$count = count($usermail);
					$this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>$count+1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$id,'type'=>5));
				}else{
					exit;
				}
			}
			//$this->mysql->query("insert mail_send(uid,mid,is_read,is_receive,send_date,update_date,rewardid,type) values($playID,$mid,0,0,$time,$time,$id,5)");
			$this->returnData(0);
		}elseif($type==2 && $sharenum!=0){
			if($invite >= $sharenum){
				$mid = $sharereward['mid'];
				//$this->mysql->query("insert mail_send(uid,mid,is_read,is_receive,send_date,update_date,rewardid,type) values($playID,$mid,0,0,$time,$time,$id,4)");
			    $usermail = $this->cache_redis->keys("hUserEmailList:".$playerID.":*");
			    if(empty($usermail)){
				    $count = 0;
				    $this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$id,'type'=>4));
			    }else{
			        $count = count($usermail);
				    $this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>$count+1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$id,'type'=>4));
			    }
				$this->returnData(0);
			}else{
				$this->returnError('301','数据异常');
			}
		}
		
		
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
